package com.lam.mall.service.impl;

import java.util.List;
import java.util.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;

import com.lam.common.base.page.TableHelper;
import com.lam.common.constant.CommonConstant;
import com.lam.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import com.lam.mall.mapper.IndexImgMapper;
import com.lam.mall.entity.IndexImg;
import com.lam.mall.param.IndexImgParam;
import com.lam.mall.service.IndexImgService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
 * 轮播图设置Service业务层处理
 * 
 * @author admin
 * @date 2023-04-20
 */
@Service
public class IndexImgServiceImpl  implements IndexImgService {

    @Autowired
    private IndexImgMapper indexImgMapper;

    /**
     * 查询轮播图设置
     * 
     * @param id ID
     * @return 单个对象
     */
    @Override
    @Transactional(readOnly = true)
    public IndexImg selectById(String id) {

        return indexImgMapper.selectById(id);
    }

    /**
     * 查询轮播图设置列表
     * 
     * @param param
     * @return 
     */
    @Override
    @Transactional(readOnly = true)
    public List<IndexImg> selectList(IndexImgParam param) {
    
        QueryWrapper<IndexImg> queryWrapper = new QueryWrapper<>();

        //关键字查询
        if(StringUtils.isNotBlank(param.getKeywords())) {
            queryWrapper.and(wr->{
                wr.or().like("title", param.getKeywords());
            });
        }
        
        queryWrapper.eq(Objects.nonNull(param.getShopId()), "shop_id", param.getShopId());
        queryWrapper.like(StringUtils.isNotBlank(param.getTitleKey()), "title", param.getTitleKey());
        queryWrapper.eq(StringUtils.isNotBlank(param.getTitle()), "title", param.getTitle());
        queryWrapper.eq(Objects.nonNull(param.getType()), "type", param.getType());
        queryWrapper.eq(Objects.nonNull(param.getStatus()), "status", param.getStatus());

        return indexImgMapper.selectList(queryWrapper);
    }

    /**
     * 新增轮播图设置
     * 
     * @param indexImg 
     * @return 
     */
    @Override
    @Transactional
    public int insert(IndexImg indexImg) {
    
        return indexImgMapper.insert(indexImg);
    }

    /**
     * 修改轮播图设置
     * 
     * @param indexImg 
     * @return 
     */
    @Override
    @Transactional
    public int update(IndexImg indexImg) {
    
        return indexImgMapper.updateById(indexImg);
    }

    /**
     * 删除轮播图设置信息
     * 
     * @param idStr ID，多个用逗号分隔
     * @return 
     */
    @Override
    @Transactional
    public int deleteById(String idStr) {
    
        if(StringUtils.isBlank(idStr)) {
            return 0;
        }
        return indexImgMapper.deleteBatchIds(StringUtils.str2List(idStr));
    }
    
    @Override
    @Cacheable(cacheNames = "indexImg", key = "'indexImg'")
    public List<IndexImg> listIndexImg() {
    	TableHelper.orderByColumn("sort_no");
    	QueryWrapper<IndexImg> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", CommonConstant.STATUS_NORMAL_1);
        return indexImgMapper.selectList(queryWrapper);
    }

    @Override
    @CacheEvict(cacheNames = "indexImg", key = "'indexImg'")
    public void removeIndexImgCache() {
    }
    
}
